package com.zhh.springcloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.zhh.springcloud.model.Department;
import com.zhh.springcloud.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

//服务提供者
@RestController
@RequestMapping("/provider")
public class DepartmentController {

    @Autowired
    DepartmentService departmentService;

    @Autowired
    private DiscoveryClient discoveryClient;

    @PostMapping("/add")
    public int add(@RequestBody Department department){
        return departmentService.add(department);
    }

    @GetMapping("/delete")     // 这里的id是限制前端表单上传的变量名称
    public int delete(@RequestParam(value = "id", required = false) int did){
        return departmentService.deleteById(did);
    }

    @PostMapping("/modify")
    public int modify(@RequestBody Department department){
        return departmentService.modifyById(department);
    }

    @GetMapping("/get")
    public Department getById(@RequestParam(value = "id", required = false) int did){
        return departmentService.selectById(did);
    }

    @SentinelResource(value = "getAll", blockHandler = "blockHandler")
    @GetMapping("/getall")
    public List<Department> getAll(){
        return departmentService.selectAll();
    }

    // 下面是sentinel异常策略的处理函数
    public static List<Department> blockHandler(BlockException e){
        Department department = new Department();
        department.setDid(0);
        department.setName("已被流量控制");
        department.setLocation("******");
        return Arrays.asList(department);
    }


}
